'use strict';
const db = uniCloud.database()
const usersCollection = db.collection('users')

exports.main = async (event, context) => {
	const { page = 1, pageSize = 10, keyword = '' } = event
	
	try {
		// 构建查询条件
		const query = {}
		if (keyword) {
			query.$or = [
				{ username: new RegExp(keyword, 'i') },
				{ mobile: new RegExp(keyword, 'i') }
			]
		}
		
		// 获取总数
		const countResult = await usersCollection.where(query).count()
		const total = countResult.total
		
		// 获取分页数据
		const res = await usersCollection
			.where(query)
			.skip((page - 1) * pageSize)
			.limit(pageSize)
			.get()
		
		return {
			code: 0,
			msg: '获取成功',
			data: {
				list: res.data,
				total,
				hasMore: page * pageSize < total
			}
		}
	} catch (e) {
		console.error('获取用户列表失败', e)
		return {
			code: -1,
			msg: '获取失败',
			error: e
		}
	}
} 